﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CookPlayer : MonoBehaviour
{

	public float m_Speed = 10f;
	public float m_Force = 10f;

	Rigidbody m_Rigidbody;
	float m_MovementInputValue;
	Vector3 dir;

    void Awake()
    {
		m_Rigidbody = transform.GetComponent<Rigidbody> ();
    }

	//	控制方向
	Vector3 GetDirection()
	{
		Vector3 dir = Vector3.zero;
		if (Input.GetKey(KeyCode.W))
		{
			dir = Vector3.forward;
		}
		else if (Input.GetKey(KeyCode.S))
		{
			dir = Vector3.back;
		}
		else if (Input.GetKey(KeyCode.A))
		{
			dir = Vector3.left;
		}
		else if (Input.GetKey(KeyCode.D))
		{
			dir = Vector3.right;
		}

		return dir;
	}

	//	移动
    void Move()
    {
		Vector3 movement = dir * m_Speed * Time.fixedDeltaTime;
    	m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
    }

	//	转向
	void Turn()
	{
		if (dir == null || dir == Vector3.zero)
		{
			return;
		}

		transform.forward = dir;
	}

	//	加速
	void AddVelocity()
	{
		 if (Input.GetKeyDown(KeyCode.Space))
		 {
			m_Rigidbody.AddForce (dir * m_Force, ForceMode.Impulse);
		 }
	}

	void Update()
	{
		dir = GetDirection();
		Turn ();
		AddVelocity();
	}

    void FixedUpdate()
    {
    	Move();

    }
}
